<?php
/**
* Django's templating engine (partial) PHP implementation
* @link http://djangoproject.com Django
* this file is from a webappkit package (kit)
* @link http://webappkit.net
* @package djtpl
* @author J.Ducastel <nospam0@ducastel.name>
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/

/**
* data context for template rendering
* @uses dataPath
* @author J.Ducastel <nospam0@ducastel.name>
*/
class djtplContext {
/*------------------------------------------------------------------------------
                                                                     constructor
------------------------------------------------------------------------------*/
    /**
    * constructor alias
    * @access
    */
    function djtplContext(&$data) {
        $this->__construct($data);
    }/**/

    /**
    * "real" constructor
    * @access public
    * @param mixed $data array or object
    */
    function __construct(&$data) {
        $this->setDataSource($data);
    }/**/
/*------------------------------------------------------------------------------
                                                                  public methods
------------------------------------------------------------------------------*/
    /**
    * sets root data source
    * @param array $data
    * @access public
    * @return bool
    */
    function setDataSource(&$data) {
        $this->data=&$data;
        return true;
    }/**/

    /**
    * gets data at targeted path
    * @param string $path
    * @access public
    * @return mixed
    */
    function getPath($path) {
        return datapath::get($this->data,$path);
    }/**/

    /**
    * sets an alias for some path
    * @param string $path
    * @param string $alias
    * @access public
    * @return bool
    */
    function setPathAlias($path, $alias) {
        // echo "<p>setting $alias as alias for $path</p>";
        // getting a reference on $path target
        $target=&datapath::get($this->data,$path); // print_r($target);
        // setting $alias as a reference on it
        datapath::setRef($this->data,$target,$alias);
        return true;
    }/**/

    /**
    * return children keys at a path
    * @param string $path
    * @access public
    * @return array
    */
    function getKeys($path) {
        return datapath::getChildKeys($this->data,$path);
    }/**/

    /**
    * Sets value for a path
    * @param string $path
    * @param mixed value
    * @access public
    * @return bool
    */
    function setPath($path,$value) {
        return datapath::set($this->data,$value,$path);
    }/**/

    /**
    * @var array source data reference
    */
    var $data;

    /**
     * @var array path aliases
     */
    //var $aliases=array();
}
